<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单id" prop="ordersId">
        <el-input
          v-model="queryParams.ordersId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单人id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入下单人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="ordersState">
        <el-input
          v-model="queryParams.ordersState"
          placeholder="请输入订单状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单来源" prop="ordersSource">
        <el-input
          v-model="queryParams.ordersSource"
          placeholder="请输入订单来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['orders:orders:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['orders:orders:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['orders:orders:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['orders:orders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="ordersId" >
        <template slot-scope="scope">
          {{scope.$index+1}}
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="ordersId" />
      <el-table-column label="下单人id" align="center" prop="userId" />
      <el-table-column label="提交时间" align="center" prop="createTime" />
      <el-table-column label="订单总价" align="center" prop="ordersPriceSum" />
      <el-table-column label="实付金额" align="center" prop="ordersPayPrice" />
      <el-table-column label="订单状态" align="center" prop="ordersState" >
        <template slot-scope="scope">
          <span v-if="scope.row.ordersState==1">未支付</span>
          <span v-if="scope.row.ordersState==2">待发货</span>
          <span v-if="scope.row.ordersState==3">已发货</span>
          <span v-if="scope.row.ordersState==4">待发货</span>
        </template>
      </el-table-column>
      <el-table-column label="订单来源" align="center" prop="ordersSource" >
        <template slot-scope="scope">
          <span v-if="scope.row.ordersSource==1">APP订单</span>
          <span v-if="scope.row.ordersSource==2">直播间订单</span>
          <span v-if="scope.row.ordersSource==3">活动抽奖订单</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="ordersPayWay">
        <template slot-scope="scope">
          <span v-if="scope.row.ordersPayWay==1">未支付</span>
          <span v-if="scope.row.ordersPayWay==2">正在支付</span>
          <span v-if="scope.row.ordersPayWay==3">支付宝</span>
          <span v-if="scope.row.ordersPayWay==4">微信</span>
          <span v-if="scope.row.ordersPayWay==5">银行卡</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['orders:orders:edit']">修改</el-button>
          <span v-if="scope.row.ordersState==1">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['orders:orders:remove']">删除订单</el-button>
          </span>
          <span v-if="scope.row.ordersState==2">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['orders:orders:remove']">订单发货</el-button>
          </span>
          <span v-if="scope.row.ordersState==3">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="confirmReceipt(scope.row)" v-hasPermi="['orders:orders:remove']">确认收货</el-button>
          </span>
          <span v-if="scope.row.ordersState==4">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['orders:orders:remove']">商品评论</el-button>
          </span>
          <el-button size="mini" type="text" icon="el-icon-view" @click="lookOrdersDetails(scope.row)" v-hasPermi="['orders:orders:ordersDetailsSelectId']">订单详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查看订单详情 -->
    <el-dialog title="订单详情" :visible.sync="dialogVisible" width="30%">
      <el-form :inline="true" :model="ordersDetails" class="demo-form-inline">
        <el-form-item label="订单编号">
          <el-input v-model="ordersDetails.ordersId" placeholder="订单编号"></el-input>
        </el-form-item>
        <el-form-item label="商品编号">
          <el-input v-model="ordersDetails.productId" placeholder="商品编号"></el-input>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model="ordersDetails.ordersDetailsProductPrice" placeholder="商品价格"></el-input>
        </el-form-item>
        <el-form-item label="商品数量">
          <el-input v-model="ordersDetails.ordersDetailsProductNum" placeholder="商品数量"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>



    </el-dialog>



    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="下单人id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入下单人id" />
        </el-form-item>
        <el-form-item label="订单总价" prop="ordersPriceSum">
          <el-input v-model="form.ordersPriceSum" placeholder="请输入订单总价" />
        </el-form-item>
        <el-form-item label="实付金额" prop="ordersPayPrice">
          <el-input v-model="form.ordersPayPrice" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="收货地址id" prop="siteId">
          <el-input v-model="form.siteId" placeholder="请输入收货地址id" />
        </el-form-item>
        <el-form-item label="快递id" prop="expressId">
          <el-input v-model="form.expressId" placeholder="请输入快递id" />
        </el-form-item>
        <el-form-item label="快递单号" prop="ordersExpressno">
          <el-input v-model="form.ordersExpressno" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="订单来源" prop="ordersSource">
          <el-input v-model="form.ordersSource" placeholder="请输入订单来源" />
        </el-form-item>
        <el-form-item label="支付方式 " prop="ordersPayWay">
          <el-input v-model="form.ordersPayWay" placeholder="请输入支付方式 " />
        </el-form-item>
        <el-form-item label="支付交易号" prop="ordersTradeno">
          <el-input v-model="form.ordersTradeno" placeholder="请输入支付交易号" />
        </el-form-item>
        <el-form-item label="支付信息" prop="ordersTradeext">
          <el-input v-model="form.ordersTradeext" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders, affirmReceiving } from "../../../api/orders/orders";
import { ordersDetailsSelectId } from "../../../api/orders/details";

export default {
  name: "Orders",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ordersId: null,
        userId: null,
        ordersState: null,
        ordersSource: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "下单人id不能为空", trigger: "blur" }
        ],
        ordersPriceSum: [
          { required: true, message: "订单总价不能为空", trigger: "blur" }
        ],
        ordersPayPrice: [
          { required: true, message: "实付金额不能为空", trigger: "blur" }
        ],
        ordersState: [
          { required: true, message: "订单状态不能为空", trigger: "blur" }
        ],
        siteId: [
          { required: true, message: "收货地址id不能为空", trigger: "blur" }
        ],
        ordersSource: [
          { required: true, message: "订单来源【1=》App订单，2=》直播订单，3=》抽奖福利订单】不能为空", trigger: "blur" }
        ],
      },
      //订单详情
      ordersDetails: {
        ordersDetailsId: null,
        ordersId: null,
        productId: null,
        ordersDetailsProductPrice: null,
        ordersDetailsProductNum: null
      },
      //对话框
      dialogVisible: false
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ordersId: null,
        userId: null,
        ordersPriceSum: null,
        ordersPayPrice: null,
        ordersState: null,
        siteId: null,
        expressId: null,
        ordersExpressno: null,
        ordersSource: null,
        ordersPayWay: null,
        ordersTradeno: null,
        isDelete: null,
        ordersTradeext: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ordersId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ordersId = row.ordersId || this.ids
      getOrders(ordersId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ordersId != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ordersIds = row.ordersId || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ordersIds + '"的数据项？').then(function() {
        return delOrders(ordersIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('orders/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    },
    /** 查询订单详情 */
    lookOrdersDetails(row) {
      ordersDetailsSelectId(row.ordersId).then(response => {
        console.log(row.ordersId)
        this.ordersDetails = response.data
      })
      this.dialogVisible = true
    },
    /** 确认收货 */
    confirmReceipt(row) {
      affirmReceiving(row.ordersId).then(response => {
        this.$message({
          message: response.msg,
          type: 'success'
        })
        this.getList();
      })
    }
  }
};
</script>
